<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>门店顾客列表</title>
	<link rel="stylesheet" href="css/public.css">
	<link rel="stylesheet" href="css/index.css">
</head>

<body>
	<div class="cont1">
		<div class="tab border">
			<p>未使用券码</p>
			<p>已使用券码</p>
		</div>
		<div class="user"></div>
	</div>
</body>
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/moment.min.js"></script>
<script src="js/vue.min.js"></script>
<script src="js/app-common.js"></script>
<script>
	$(function () {
		var allData = [];
		var productId = window.localStorage.getItem("product_id");
		var storeId = window.localStorage.getItem("chooseId").split(",");
		var storeName = window.localStorage.getItem("chooseName").split(",");
		var isUsed = 0;
		onceGetData(productId, storeId, isUsed);
		$(".tab p").on("click", function () {
			if ($(this).text() == "未使用券码") {
				allData = [];
				$(".userInfo1").show();
				$(".userInfo2").hide();
				isUsed = 0;
				onceGetData(productId, storeId, isUsed);
			} else {
				allData = [];
				$(".userInfo2").show();
				$(".userInfo1").hide();
				isUsed = 1;
				onceGetData(productId, storeId, 1);
			}
			$(this).css("color", "#ff9340");
			$(this).css("border-bottom", "2px solid #ff9340");
			$(this).siblings().css("color", "#666");
			$(this).siblings().css("border-bottom", "none");
		});

		//获取全部数据
		function onceGetData(productId, storeId, isUsed) {
			for (var i = 0; i < storeId.length; i++) {
				loadData(productId, storeId[i], isUsed);
			}
		}

		//获取每个门店的数据
		function loadData(productId, storeId, isUsed) {
			$.ajax({
				type: "POST",
				url: APP_CONFIG.listOrderByStoreUrl,
				data: {
					token: getUserToken(),
					product_id: productId,
					store_id: storeId, // 逗号分隔列表
					is_used: isUsed // 0-查询未使用; 1-查询已使用
				},
				success: function (res) {
					if (checkServiceResult(res)) {
						allData.push({ store_name: storeName[0], info: res.data });
						getData(allData);
	console.log(allData);
					}
				}
			});
		}

		//填充页面
		function getData(allData) {
			for (var n in allData) {
				allData[n].store_name = storeName[n];
			}
			var str = "";
			if (isUsed == 1) {
				for (var a in allData) {
					if (allData[a].info.length > 0) {
						str += "<div class='userInfo ab'>" +
							"<h4 class='title border'>" + allData[a].store_name + "</h4>" +
							"<table class='info_wrap'>";
						for (var b in allData[a].info) {
							str += "<tr class='cus_info border_none'><td>" + allData[a].info[b].customer_name + "<br><i>顾客姓名</i></td><td>" + allData[a].info[b].phone + "<br><i>手机号码</i></td><td class='bought'>" + moment(allData[a].info[b].pay_time).format("YYYY-MM-DD") + "<br><i>购买时间</i></td></tr><tr class='cus_info used_time_wrap'><td></td><td></td><td class='used_time'>" + moment(allData[a].info[b].used_time).format("YYYY-MM-DD") + "<br><i>使用时间</i></td></tr>";
						}
					} else {
						str += "<div class='userInfo ab'>" +
							"<h4 class='title border'>" + allData[a].store_name + "</h4>" +
							"<table class='info_wrap'>" +
							"<tr><td>该门店无数据</td></tr>";
					}

					str += "</table></div>";
				}
			} else {
				for (var a in allData) {
					if (allData[a].info.length > 0) {
						str += "<div class='userInfo ab'>" +
							"<h4 class='title border'>" + allData[a].store_name + "</h4>" +
							"<table class='info_wrap'>";
						for (var b in allData[a].info) {
							str += "<tr class='cus_info'><td>" + allData[a].info[b].customer_name + "<br><i>顾客姓名</i></td><td>" + allData[a].info[b].phone + "<br><i>手机号码</i></td><td class='bought'>" + moment(allData[a].info[b].pay_time).format("YYYY-MM-DD") + "<br><i>购买时间</i></td></tr>"
						}
					} else {
						str += "<div class='userInfo ab'>" +
							"<h4 class='title border'>" + allData[a].store_name + "</h4>" +
							"<table class='info_wrap'>" +
							"<tr><td>该门店无数据</td></tr>";
					}

					str += "</table></div>";
				}
			}

			$(".user").html(str);
		}
	});
</script>

</html>